package com.diqizu.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.diqizu.entity.News;
import com.diqizu.utli.DBManager;
import com.diqizu.utli.QueryDataResult;



public class NewsDaoImpl {
  /**
   * 所有新闻列表
   * @return
   */
  public ArrayList<News> getAllList() {
    String sql = "select * from news order by time desc";
    System.out.println("执行的sql语句是:" + sql);
    QueryDataResult execQuery = DBManager.execQuery(sql);
    ArrayList<News> list = new ArrayList<News>();
    if (execQuery != null) {
      ResultSet rs = execQuery.rs;
      try {
        while (rs.next()) {
          News e = new News(rs.getInt("id"),rs.getString("title"),rs.getString("context"),rs.getInt("typeid"),rs.getString("time"),rs.getString("isShow"));
          list.add(e);
        }
      } catch (SQLException e) {
        e.printStackTrace();
      } finally {
        execQuery.close();
      }
    }
    System.out.println("查到的数据是："+list);
    return list;
  }
  
  
  /**
   * 通过类型id获取 新闻列表
   * @param typeId
   * @return
   */
  public ArrayList<News> getListByTypeId(String typeid) {
    String sql = "select * from news";
    if (typeid != null && !typeid.equals("")) {
    sql = sql + " where typeid='" + typeid + "' ";
  }
    System.out.println("执行的sql语句是:" + sql);
    QueryDataResult execQuery = DBManager.execQuery(sql);
    ArrayList<News> list = new ArrayList<News>();
    if (execQuery != null) {
      ResultSet rs = execQuery.rs;
      try {
        while (rs.next()) {
        	News e = new News(rs.getInt("id"),rs.getString("title"),rs.getString("context"),rs.getInt("typeid"),rs.getString("time"),rs.getString("isShow"));
          list.add(e);
        }
      } catch (SQLException e) {
        e.printStackTrace();
      } finally {
        execQuery.close();
      }
    }
    System.out.println("查到的数据是："+list);
    return list;
  }
  
  
  
  /**
   * 返回6条数据
   * @return
   */
  public ArrayList<News> getListLimit6() {
    String sql = "select * from news order by time desc limit 6";
    System.out.println("执行的sql语句是:" + sql);
    QueryDataResult execQuery = DBManager.execQuery(sql);
    ArrayList<News> list = new ArrayList<News>();
    if (execQuery != null) {
      ResultSet rs = execQuery.rs;
      try {
        while (rs.next()) {
        	News e = new News(rs.getInt("id"),rs.getString("title"),rs.getString("context"),rs.getInt("typeid"),rs.getString("time"),rs.getString("isShow"));
          list.add(e);
        }
      } catch (SQLException e) {
        e.printStackTrace();
      } finally {
        execQuery.close();
      }
    }
    System.out.println("查到的数据是："+list);
    return list;
    
	
	
    
    
    
  }

/**
 * 后台用的
 */
  
  
  /**
   * 增加方法
   * @param Banner
   * @return
   */
  public boolean insertNews(News news) {
    String sql = "insert into news (title,context,typeid,time,isShow) values('"+news.getTitle()+"','" + news.getContext() + "','" + news.getNewid()+ "','" + news.getTime() + "','" + news.getIsShow()+ "')";
    System.out.println(sql);
    boolean i = DBManager.execUpdate(sql);
    if(i==true) {
      return true;
    }
    return false;
  }
  
  /**
   * 修改方法
   * @param Banner
   * @return
   */
  public boolean updateNews(News news) {
    String sql = "update news set title = '"+news.getTitle()+"',context ='" + news.getContext() + "',typeid ='" + news.getNewid() + "',time ='" + news.getTime() + "',isShow ='" + news.getIsShow() + "' where id = " + news.getId();
    System.out.println(sql);
    boolean i = DBManager.execUpdate(sql);
    if(i==true) {
      return true;
    }
    return false;
  }
  
  /**
   * 删除方法
   * @param Banner
   * @return
   */
  public boolean delNews(int id) {
    String sql = "delete from news where id =" +id;
    System.out.println(sql);
    boolean i = DBManager.execUpdate(sql);
    if(i==true) {
      return true;
    }
    return false;
  }
}

